/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link class="back" :to="{name:'member_levels'}" title="返回列表">
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>会员等级管理 - 编辑会员等级</h3>
            <h5>网站系统抢会员等级管理</h5>
          </div>
        </div>
      </div>
      <form :model="form">
        <div class="ncap-form-default">
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>等级名称
              </label>
            </dt>
            <dd class="opt">
              <input v-model="form.level_name" type="text" class="input-txt" />
              <p
                class="err-msg"
                v-if="errResult.level_name && !form.level_name"
              >{{errResult.level_name}}</p>
              <p class="notic">设置等级名称</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>消费额度
              </label>
            </dt>
            <dd class="opt">
              <input v-model="form.amount" type="text" class="input-txt" />
              <p class="err-msg" v-if="errResult.amount">{{errResult.amount}}</p>
              <p class="notic">设置会员等级所需要的消费额度,单位：元</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>折扣率</label>
            </dt>
            <dd class="opt">
              <input
                v-model="form.discount"
                type="text"
                onpaste="this.value=this.value.replace(/[^\d.]/g,'')"
                onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
                class="input-txt"
              />
              <p class="notic">折扣率单位为百分比，如输入90，表示该会员等级的用户可以以商品原价的90%购买</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>等级描述</label>
            </dt>
            <dd class="opt">
              <textarea v-model="form.describe" rows="6" class="tarea"></textarea>
              <p class="notic">会员等级描述信息</p>
            </dd>
          </dl>

          <div class="bot">
            <el-button type="primary" @click="onSubmit()" :loading="isLoading">确认提交</el-button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { getUserLevel, updateUserLevel, addUserLevel } from "@/utils/api";
export default {
  data() {
    return {
      id: this.$route.params.id,
      form: {},
      isLoading: false,
      errResult: {}
    };
  },
  created() {
    if (this.id) {
      this.getMemberLevel();
    }
  },
  methods: {
    getMemberLevel() {
      var that = this;
      getUserLevel({
        level_id: this.id
      }).then(function(res) {
        that.form = res;
      });
    },
    onSubmit() {
      this.isLoading = true;
      var that = this;
      if (this.form.level_id) {
        //编辑
        updateUserLevel(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              duration: 1000,
              onClose: function() {
                that.$router.push({
                  name: "member_levels"
                });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        addUserLevel(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              duration: 1000,
              onClose: function() {
                that.$router.push({ name: "member_levels" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
</style>
